package jnpf.ureport.cell.right;

import jnpf.ureport.build.Context;
import jnpf.ureport.cell.DuplicateType;
import jnpf.ureport.definition.BlankCellInfo;
import jnpf.ureport.definition.value.SimpleValue;
import jnpf.ureport.definition.value.Value;
import jnpf.ureport.model.Cell;
import jnpf.ureport.model.Column;
import jnpf.ureport.utils.DataUtils;
import lombok.Getter;
import lombok.Setter;

/**
 * @author
 * @since 2016年11月7日
 */
@Getter
@Setter
public class CellRightDuplicator {
    private Cell cell;
    private int cellColNumber;
    private DuplicateType duplicateType;
    private BlankCellInfo blankCellInfo;
    private boolean nonChild = false;

    public CellRightDuplicator(Cell cell, DuplicateType duplicateType, int cellColNumber) {
        this.cell = cell;
        this.duplicateType = duplicateType;
        this.cellColNumber = cellColNumber;
    }

    public CellRightDuplicator(Cell cell, DuplicateType duplicateType, BlankCellInfo blankCellInfo, int cellColNumber) {
        this.cell = cell;
        this.duplicateType = duplicateType;
        this.blankCellInfo = blankCellInfo;
        if (cellColNumber > 0) {
            this.cellColNumber = cellColNumber;
        } else {
            this.cellColNumber = cell.getColumn().getColumnNumber();
        }
    }


    public Cell duplicateChildrenCell(RightDuplicate rightDuplicate, Cell topParent, Cell originalCell, boolean parentNonChild) {
        Cell newCell = cell.newCell();
        Column newCol = rightDuplicate.newColumn(newCell.getColumn(), cellColNumber);
        newCol.getCells().add(newCell);
        newCell.getRow().getCells().add(newCell);
        newCell.setColumn(newCol);
        if (newCell.getTopParentCell() == originalCell) {
            newCell.setTopParentCell(topParent);
            if (parentNonChild) {
                nonChild = true;
            }
        } else {
            nonChild = true;
        }
        Cell leftParentCell = newCell.getLeftParentCell();
        if (leftParentCell != null) {
            leftParentCell.addRowChild(newCell);
        }
        Cell topParentCell = newCell.getTopParentCell();
        if (topParentCell != null) {
            topParentCell.addColumnChild(newCell);
        }
        Context context = rightDuplicate.getContext();
        Value value = newCell.getValue();
        DataUtils.cellList(newCell, true, true);
        if (value instanceof SimpleValue) {
            newCell.setData(value.getValue());
            newCell.setProcessed(true);
            context.addReportCell(newCell);
        } else {
            if (nonChild) {
                newCell.setValue(new SimpleValue(""));
            } else {
                context.addCell(newCell);
            }
        }
        return newCell;
    }

    public Cell duplicate(RightDuplicate rightDuplicate, Cell newMainCell) {
        switch (duplicateType) {
            case Blank:
                processBlankCell(rightDuplicate, newMainCell);
                break;
            case Self:
                processSelfBlankCell(rightDuplicate);
                break;
            case IncreaseSpan:
                processIncreaseSpanCell(rightDuplicate);
                break;
        }
        return null;
    }

    private void processBlankCell(RightDuplicate rightDuplicate, Cell newMainCell) {
        Context context = rightDuplicate.getContext();
        Cell newBlankCell = cell.newColumnBlankCell(context, blankCellInfo, rightDuplicate.getMainCell());
        if (blankCellInfo.isParent() && newMainCell.getTopParentCell() == cell) {
            newMainCell.setTopParentCell(newBlankCell);
        }
        Column col = rightDuplicate.newColumn(newBlankCell.getColumn(), cellColNumber);
        col.getCells().add(newBlankCell);
        newBlankCell.getRow().getCells().add(newBlankCell);
        newBlankCell.setColumn(col);
        context.addReportCell(newBlankCell);
    }

    private void processSelfBlankCell(RightDuplicate rightDuplicate) {
        Cell newBlankCell = cell.newCell();
        newBlankCell.setValue(new SimpleValue(""));
        Column newCol = rightDuplicate.newColumn(newBlankCell.getColumn(), cellColNumber);
        newCol.getCells().add(newBlankCell);
        newBlankCell.getRow().getCells().add(newBlankCell);
        newBlankCell.setColumn(newCol);
        Cell leftParentCell = newBlankCell.getLeftParentCell();
        if (leftParentCell != null) {
            leftParentCell.addRowChild(newBlankCell);
        }
        Cell topParentCell = newBlankCell.getTopParentCell();
        if (topParentCell != null) {
            topParentCell.addColumnChild(newBlankCell);
        }
        Context context = rightDuplicate.getContext();
        context.addBlankCell(newBlankCell);
    }

    private void processIncreaseSpanCell(RightDuplicate rightDuplicate) {
        int colSpan = cell.getColSpan();
        colSpan += rightDuplicate.getColumnSize();
        if (colSpan == 1) {
            colSpan++;
        }
        cell.setColSpan(colSpan);
    }


}
